<?php

namespace App\Http\Controllers\Mobile;


use App\Repos\ProductRepo;
use App\Repos\UserRepo;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Repos\WxUserRepo;

class CartController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {

        $wechatUser  = session('wechat.oauth_user.default');
        $userInfo    = WxUserRepo::loginOrRegister($wechatUser);

        $user        = UserRepo::findByOpenid($wechatUser->id);

        $userId = $user->id;
        $where['productid'] = '';
        $where['spescids']  = [];

        if($request->productid){
            $where['productid'] = $request->productid;
        }


        if($request->spescids){
            $where['spescids'] = explode(',',$request->spescids);
        }

        if($request->num){
            $num = $where['num'] = (int)$request->num;
        }

        $cartTemp =  ProductRepo::getProductSpecsDetails($where['productid'],$where['spescids']);

        return view('mobile.cart',compact('cartTemp','num','userId'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }

    public function cartValidate(Request $request)
    {

        $where['id']     = $request->productId;
        $product         = ProductRepo::find($where);
        $productSpescIds = $request->filled('productSpescIds');

        //商品是否有规格
        if(array_get($product,'has_spec') != 0 ){
            //是否选择商品规格
            if($productSpescIds){
                //商品规格数
                $productSpecsNum   = ProductRepo::getProductSpecs((int)$request->productId)->pluck("spec_id")->unique()->count();
                //校验是否已选择规格
                if($productSpecsNum !=  count($request->productSpescIds)){
                    return response()->json(['status' =>'danger', 'msg' => '请选择商品规格', 'data' => []]);
                }
            }
        }

        //校验商品库存
        /*$buyNum = $request->filled('buyNum');
        if($buyNum){
            $stockNum = ProductRepo::isNormalStock($request->productSpescIds);

            //选择商品数量大于库存时
            if($stockNum < $request->buyNum){
                return response()->json(['status' =>'danger', 'msg' => '抱歉，商品库存不足', 'data' => []]);
            }
        }else{
            return response()->json(['status' =>'danger', 'msg' => '请选择商品数量', 'data' => []]);
        }*/


        //商品属性
        $psids = $request->productSpescIds ? $request->productSpescIds : null;
        $cartTemp =  ProductRepo::getProductSpecsDetails($request->productId,$psids);

        //加入购物车
        if($request->clickType == 'addToCart'){
            return response()->json(['status' =>'success01', 'msg' => 'ok', 'data' => $cartTemp]);
        }

        //立即购买
        if($request->clickType == 'immedBuy'){
            return response()->json(['status' =>'success02', 'msg' => 'ok', 'data' => $cartTemp]);
        }


        return response()->json(['status' =>'success', 'msg' => 'ok', 'data' => $cartTemp]);

    }

    public function cartConfirm(Request $request)
    {
        //校验商品库存
        /*$buyNum = $request->filled('buyNum');

        if($buyNum){

            $stockNum = ProductRepo::isNormalStock(explode(',',$request->productSpescIds));
            //选择商品数量大于库存时
            if($stockNum < $request->buyNum){
                return response()->json(['status' =>'danger', 'msg' => '抱歉，商品库存不足', 'data' => []]);
            }
        }*/

        if($request->buyNum <= 0 ){
            return response()->json(['status' =>'danger', 'msg' => '商品数量不合法', 'data' => []]);
        }

        return response()->json(['status' =>'success', 'msg' => 'ok', 'data' => []]);

    }
}
